//
//  CarSelectCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/28/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



protocol CarSelectProtocol {
    func didSelectCar (indexPath: IndexPath)
}


final class CarSelectCell: UITableViewCell {
    
    // MARK: - Outlets
    lazy private var iconImg: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        img.image = UIImage(named: "machine")
        return img
    }()
    lazy private var titleLbl: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        label.textAlignment = .left
        label.textColor = .mediumDarkBlue
        return label
    }()
    lazy private var carNumberView: CarNumberView = {
        let view = CarNumberView()
        return view
    }()
    lazy private var checkBox: UIButton = {
        let btn = UIButton()
        btn.layer.borderColor = UIColor.greyBlue.cgColor
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 10
        self.deselectCheckbox(btn)
        return btn
    }()
    
    // MARK: - Properties
    
    var carModel: CarModel! {
        didSet {
            self.carNumberView.updateUI(with: carModel)
            self.titleLbl.text = carModel.carName
            
            if (carModel.isSelected) {
                self.selectCheckbox(checkBox)
            } else {
                self.deselectCheckbox(checkBox)
            }
        }
    }
    var delegate: CarSelectProtocol!
    var indexPath: IndexPath!
    // MARK: - Actions
    
    @objc private func handleCheckboxPress (sender: UIButton) {
        delegate.didSelectCar(indexPath: indexPath)
    }
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
        self.addUIConstratints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func selectCheckbox (_ button: UIButton) {
        button.layer.borderWidth = 0
        button.setImage(UIImage(named: "selected"), for: .normal)
    }
    private func deselectCheckbox (_ button: UIButton) {
        button.layer.borderWidth = 1
        button.setImage(nil, for: .normal)
    }
    
    
    private func addViews() {
        self.contentView.addSubview(iconImg)
        self.contentView.addSubview(titleLbl)
        self.contentView.addSubview(carNumberView)
        self.contentView.addSubview(checkBox)
        checkBox.addTarget(self, action: #selector(handleCheckboxPress(sender:)), for: .touchUpInside)
    }
    private func addUIConstratints () {
        iconImg.snp.makeConstraints { (make) in
            make.width.equalTo(18)
            make.height.equalTo(14)
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        checkBox.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-16)
            make.width.height.equalTo(20)
            make.centerY.equalToSuperview()
        }
        carNumberView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(checkBox.snp.leading).offset(-25)
            make.height.equalTo(26)
            make.width.equalTo(120)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.leading.equalTo(iconImg.snp.trailing).offset(20)
            make.centerY.equalToSuperview()
            make.trailing.equalTo(carNumberView.snp.leading)
        }
    }
}
